package com.wskeee.ec.containers
{
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.interfaces.IContainer;
	import com.wskeee.ec.styles.StyleNames;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class Container extends EComponent implements IContainer
	{
		protected var _showBorder:Boolean = false;
		protected var borderDrawer:Graphics = graphics;
		
		public function Container()
		{
			super();
		}
		//========================= override ====================
		//---------------------
		//override displayObject
		//---------------------
		override public function addChild(child:DisplayObject):DisplayObject
		{
			addChildAt(child,numChildren);
			return child;
		}
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			super.addChildAt(child,index);
			addChildEvent(child);
			invalidateSize();
			return child;
		}
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			return removeChildAt(getChildIndex(child));
		}
		override public function removeChildAt(index:int):DisplayObject
		{
			var d:DisplayObject = super.removeChildAt(index);
			removeChildEvent(d);
			invalidateSize();
			return d;
		}
		
		private function addChildEvent(child:DisplayObject):void
		{
			child.addEventListener(Event.RESIZE,onChildResize);
		}
		
		protected function onChildResize(event:Event):void
		{
			invalidateSize();
		}
		
		private function removeChildEvent(child:DisplayObject):void
		{
			if(child && child.hasEventListener(Event.RESIZE))
			child.removeEventListener(Event.RESIZE,onChildResize);
		}
		//========================= end =========================
		
		//========================= validate ====================
		override protected function measure():void
		{
			super.measure();
			var contentRect:Rectangle = this.getBounds(this);
			measureWidth = contentRect.width + _paddingLeft + _paddingRight;
			measureHeight = contentRect.height + _paddingTop + _paddingBottom;
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			if(border)
				borderDrawer.clear();
		}
		//========================= end =========================
		
		//========================= act =========================

		//========================= end =========================
		
		
		//========================= styles ======================
		//---------------------
		//IContainer
		//---------------------
		protected var _verticalGap:Number = 2;
		protected var _horizontalGap:Number = 2;
		protected var _paddingTop:Number = 0;
		protected var _paddingLeft:Number = 0;
		protected var _paddingRight:Number = 0;
		protected var _paddingBottom:Number = 0;
		protected var _horizontalAlign:String = "left";
		protected var _verticalAlign:String = "top";
		
		public function get verticalGap():Number
		{
			return _verticalGap;
		}
		public function set verticalGap(val:Number):void
		{
			_verticalGap = val;
			invalidateSize();
		}
		
		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}
		public function set horizontalGap(val:Number):void
		{
			_horizontalGap = val;
			invalidateSize();
		}
		
		public function get paddingTop():Number
		{
			return _paddingTop;
		}
		public function set paddingTop(val:Number):void
		{
			_paddingTop = val;
			invalidateSize();
		}
		
		public function get paddingLeft():Number
		{
			return _paddingLeft;
		}
		public function set paddingLeft(val:Number):void
		{
			_paddingLeft = val;
			invalidateSize();
		}
		
		public function get paddingRight():Number
		{
			return _paddingRight;
		}
		public function set paddingRight(val:Number):void
		{
			_paddingRight = val;
			invalidateSize();
		}
		
		public function get paddingBottom():Number
		{
			return _paddingBottom;
		}
		public function set paddingBottom(val:Number):void
		{
			_paddingBottom = val;
			invalidateSize();
		}
		
		[Inspectable(category="General", enumeration="left,right,center", defaultValue="left")]
		public function get horizontalAlign():String
		{
			return _horizontalAlign;
		}
		public function set horizontalAlign(value:String):void
		{
			_horizontalAlign = value;
			invalidateSize();
		}
		
		[Inspectable(category="General", enumeration="top,bottom,middle", defaultValue="top")]
		public function get verticalAlign():String
		{
			return _verticalAlign;
		}
		public function set verticalAlign(value:String):void
		{
			_verticalAlign = value;
			invalidateSize();
		}
		
		public function set borderColor(value:Object):void
		{
			setStyle(StyleNames.BORDER_COLOR,value);
			invalidateSize();
		}
		
		public function get borderColor():Object
		{
			return getStyle(StyleNames.BORDER_COLOR);
		}
		
		public function set clip(bo:Boolean):void
		{
		}
		
		public function get clip():Boolean
		{
			return false;
		}
		//========================= end =========================
	}
}